package com.zl.learning.lambada;

import com.zl.learning.lambada.model.User;
import org.junit.Test;

import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class FieldNameTest {

    @Test
    public void test1() {
        User user = new User();
//        user.setId(2);
        int id = Optional.ofNullable(user).map(User::getId).orElse(1);
        System.out.println(id);
    }

    protected <R> String columnToString(R column) {
        if (column instanceof String) {
            return (String) column;
        }
        return "aa";
    }

    public <T> void change(Function<T, Integer> get, BiConsumer<T, Integer> set, T obj) {
        Integer id = get.apply(obj);
        System.out.println(id);
        set.accept(obj, 3);
    }

    @Test
    public void test2() {
        User user = new User();
        user.setId(1);
        BiConsumer<User, Integer> v = User::setId;
        change(User::getId, User::setId, user);
        System.out.println(user.getId());
    }


    @Test
    public void testFieldName() {
        String s = FnConverter.of(User.class).getFieldName(User::getPassword);
        System.out.println(s); //age
    }

    @Test
    public void test3() {
        User user = new User();
        Optional.of(user).filter(r -> r.getUsername() == null || r.getUsername().isEmpty()).ifPresent(r -> user.setUsername("zhangsan"));
//        Optional.of(user).map(User::getUsername).ifPresent(r -> user.setUsername("zhangsan"));
        System.out.println(user);
    }
}
